package angels.client.Activities;

import com.readystatesoftware.mapviewballoons.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import angels.client.Entities.User;
import angels.client.Listeners.UserListener;
import angels.client.Services.UserService;

public class UserActivity extends Activity implements UserListener,ServiceConnection {

    TextView title;
    TextView body;
    ImageView image;
    RatingBar rating;
    UserService service;
    private Handler handler = new Handler();
    
    public void onServiceConnected(ComponentName className, IBinder binder) {
		service = ((UserService.LocalBinder) binder).getService();
		Bundle extras = getIntent().getExtras(); 
        String username=(String)extras.get("title");
		service.getUser(username, this);
	}

	public void onServiceDisconnected(ComponentName className) {
	    service = null;
	}
    
    @Override	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        rating =new RatingBar(this);
        image=new ImageView(this);
     
        title = new TextView(this);
        title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        title.setGravity(Gravity.TOP);
        title.setClickable(false);
        title.setFocusable(false);
        title.setTextSize(30);
        
        rating.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        rating.setNumStars(5);
        //rating.setRating(2);
        
        rating.setClickable(false);
        
        body = new TextView(this);
        body.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        body.setGravity(Gravity.TOP);
        body.setClickable(false);
        body.setFocusable(false);
        
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        
        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        innerLayout.addView(image);
        innerLayout.addView(title);
        
        layout.addView(innerLayout);
        layout.addView(rating);
        layout.addView(body);
        setContentView(layout);
        
        Bundle extras = getIntent().getExtras(); 
        String username=(String)extras.get("title");
        title.append(username);
        body.setText("Loading Info...");
        image.setImageResource(R.drawable.icon);
        image.setAdjustViewBounds(true);
        image.setMaxHeight(100);
        
        Intent userService = new Intent(this, UserService.class);
        getApplicationContext().bindService(userService, this, Context.BIND_AUTO_CREATE);
    }

	@Override
	public void onUserInfoReceived(final User user) {
		handler.post(new Runnable() {	
            public void run() {
            	rating.setRating(user.getRating());
            	body.setText(user.getDescription());
            	service.getUserImage(image, user.getImageURL());
            }
        });
	}

	@Override
	public void onUserInfoSent() {}
	
}